﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ClassForWinForm
{
    public partial class Form9 : Form
    {
        DataTable dt_file = new DataTable();
        int Seq = 0;
        public Form9()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog f = new FolderBrowserDialog();
            //设置打开对话框的标题
            f.Description = "请选择需要预览的文件夹";
            if (f.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = f.SelectedPath;
                GetFiles(f.SelectedPath);
                if (dt_file.Rows.Count>0)
                {
                    pictureBox1.ImageLocation = dt_file.Rows[0][0].ToString();
                }
            }
        }

        private void Form9_Load(object sender, EventArgs e)
        {
            dt_file.Columns.Add("FileName");
        }
        public void GetFiles(string FilePath)
        {
            DirectoryInfo folder = new DirectoryInfo(FilePath);
            foreach (FileInfo f in folder.GetFiles())
            {
                string type = f.Extension.ToLower();
                if (type.Equals(".gif") || type.Equals(".jpge") || type.Equals(".jpg") || type.Equals(".png"))
                {
                    DataRow dr = dt_file.NewRow();
                    dr["FileName"] = f.FullName;
                    dt_file.Rows.Add(dr);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (Seq == 0)
            {
                MessageBox.Show("已经是第一张了！");
            }
            else
            {
                Seq -= 1;
                pictureBox1.ImageLocation = dt_file.Rows[Seq][0].ToString();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            
            if (Seq + 1 < dt_file.Rows.Count)
            {
                Seq += 1;
                pictureBox1.ImageLocation = dt_file.Rows[Seq][0].ToString();
            }
            else
            {
                MessageBox.Show("已经没有了！");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if(dt_file.Rows.Count==0)
            {
                MessageBox.Show("当前没有可播放图片！！");
                return;
            }
            timer1.Interval = 1000;
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Seq + 1 == dt_file.Rows.Count)
            {
                Seq = 0;
            }
            else
            {
                Seq += 1;
            }
            
            pictureBox1.ImageLocation = dt_file.Rows[Seq][0].ToString();
        }
    }
}
